package exam.controller;


import cn.hutool.http.HtmlUtil;
import exam.bean.ProjectInfo;
import exam.service.IProjectInfoService;
import exam.utils.FileUtil;
import exam.utils.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@RestController()
@RequestMapping("/ProjectInfo")
public class ProjectInfoController {
    @Autowired
    IProjectInfoService projectInfoService;

    @Value("${upload.uploadPath}")
    private String uploadPath;


    @PreAuthorize("hasAuthority('sys:project:list')")
    @ResponseBody
    @PostMapping("/searchInfo/{pageNum}/{pageSize}")
    public Msg searchInfo(@PathVariable String pageNum,@PathVariable String pageSize,@RequestBody Map queryMap){
        return Msg.success().add("pageInfo",projectInfoService.selectInfo(Integer.parseInt(pageNum),
                Integer.parseInt(pageSize),queryMap));
    }

    @PreAuthorize("hasAuthority('sys:project:add')")
    @PostMapping("/add")
    public Msg add( @RequestPart("projectInfo") ProjectInfo projectInfo,
                        @RequestPart("file") MultipartFile[] file){
        if(projectInfo.getSpecifications() != null && "".equals(projectInfo.getSpecifications())){
            projectInfo.setSpecifications(HtmlUtils.htmlEscape(projectInfo.getSpecifications()));
        }

        projectInfo.setUtilFiles(Arrays.asList(file));
        boolean re = projectInfoService.insertInfo(projectInfo,uploadPath) > 0 ;
        return Msg.success().setMessage(re ? "添加成功":"添加失败");
    }

    @PreAuthorize("hasAuthority('sys:project:detail')")
    @ResponseBody
    @GetMapping("/detail")
    public Msg detail(@RequestParam(value="id") int id){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        return Msg.success().add("projectInfo",projectInfoService.selectInfoSingle(map));
    }

    @PreAuthorize("hasAuthority('sys:project:delete')")
    @ResponseBody
    @GetMapping("/delete")
    public Msg delete (@RequestParam(value="id") int id){
        boolean re = projectInfoService.deleteInfo(id) > 0 ;
        return Msg.success().setMessage(re ? "删除成功":"删除失败");
    }

    @PreAuthorize("hasAuthority('sys:project:update')")
    @ResponseBody
    @PostMapping("/upData")
    public Msg update(@RequestPart("projectInfo") ProjectInfo projectInfo,
                      @RequestPart(value = "files",required = false) MultipartFile[] files){
        projectInfo.setUtilFiles(Arrays.asList(files));
        if(projectInfo.getSpecifications() != null && "".equals(projectInfo.getSpecifications())){
            projectInfo.setSpecifications(HtmlUtils.htmlEscape(projectInfo.getSpecifications()));
        }
        boolean re = projectInfoService.updateInfo(projectInfo,uploadPath) > 0;
        return Msg.success().setMessage(re ? "修改成功":"修改失败");
    }

    @GetMapping("/downLoad")
    public void downLoad(HttpServletResponse response, String fileName, String fileNameSrc){
        FileUtil.downLoadFile(response,uploadPath,fileName,fileNameSrc);
    }

    @GetMapping("/projectList")
    public Msg projectList(){
        return Msg.success().add("projectList",projectInfoService.projectList());
    }


    /**
     * 获得当前项目的余额
     */
    @GetMapping("/remainPrice")
    public Msg projectRemainPrice(int id){
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("id",id);
        return Msg.success().add("remainPrice",projectInfoService.selectInfoSingle(queryMap).getRemainPrice());
    }
}
